<template>
    <div>
      <header class="header">
        <div class="navbar">
          <div class="navbar-left-container">
            <a href="/">
              <img class="navbar-brand-logo" src="../../static/logo.jpg"></a>
          </div>
          <span class="titleFont">vue+node+mongo迷你商城Demo</span>
          <div class="navbar-right-container" style="display: flex;">
            <div class="navbar-menu-container">
              <span class="navbar-link"></span>
              <!--昵称-->
              <span   v-if="nickName"> {{nickName}} </span>
              <a href="javascript:void(0)"  v-if="!nickName" @click="loginModalFlag=true" class="navbar-link">登陆 </a>
              <a href="javascript:void(0)" @click="logout" class="navbar-link"  v-if="nickName">登出</a>
              <a class="cartLogo" href="/#/cart"><Icon type="ios-cart"></Icon></a>
            </div>
          </div>
        </div>
        <div class="md-modal modal-msg md-modal-transition" :class="{'md-show':loginModalFlag}" >
          <div class="md-modal-inner">
            <div class="md-top">
              <div class="md-title">Login in</div>
              <button class="md-close" @click="loginModalFlag=false">Close</button>
            </div>
            <div class="md-content">
              <div class="confirm-tips">
                <div class="error-wrap">
                  <span class="error error-show" v-show="errorTip">用户名或者密码错误</span>
                </div>
                <ul>
                  <li class="regi_form_input">
                    <i class="icon IconPeople"></i>
                    <input type="text" tabindex="1" name="loginname" v-model="userName" class="regi_login_input regi_login_input_left" placeholder="User Name" data-type="loginname">
                  </li>
                  <li class="regi_form_input noMargin">
                    <i class="icon IconPwd"></i>
                    <input type="password" tabindex="2"  name="password" v-model="userPwd" class="regi_login_input regi_login_input_left login-input-no input_text" placeholder="Password" @keyup.enter="login">
                  </li>
                </ul>
              </div>
              <div class="login-wrap">
                <a href="javascript:;" class="btn-login" @click="login">登  录</a >
              </div>
            </div>
          </div>
        </div>
        <div class="md-overlay" v-if="loginModalFlag"></div>
      </header>
    </div>
</template>

<script>
  import './../assets/css/login.css'

    export default{
        data(){
          return {
              userName:'admin',
              userPwd:'123456',
              errorTip:false,
              loginModalFlag:false,
              nickName:''
          }
        },
      mounted(){
        this.checkLogin()
      },
      methods:{
          //登录校验
            checkLogin(){
              this.$http.get('/users/checkLogin')  .then(response=>{
                  let res = response.data;
                  if(res.status == '0'){
                    this.nickName = res.result
                  }
              })
            },

            login(){
                if(!this.userName || !this.userPwd){
                  this.errorTip = true;
                  return
                }
               this.$http.post('/users/login',{
                    userName:this.userName,
                    userPwd:this.userPwd,
                }).then(response=>{
                    let res = response.data;
                 console.log(res);
                    if(res.status =='0'){
                        this.errorTip = false;
                        this.loginModalFlag = false;
                        this.nickName = res.result.userName
                    }else{
                      this.errorTip = true;
                    }
                })
            },
        logout(){
          if(confirm('确认退出？')){
            this.$http.post('/users/logout').then(response=>{
              let res = response.data;
              if(res.status == '0'){
                this.nickName = '';
              }
            })
          }
        }
      }
    }
</script>

<style scoped>
.cartLogo{
  margin-left: 15px;
  color: green;
  transform:scale(1.5)
}
</style>
